import 'package:flutter/material.dart';
import 'package:flutter_house_manager/pages/House/house_detail.dart';
import 'package:flutter_house_manager/pages/House/house_list.dart';
import 'package:flutter_house_manager/pages/Location/build_list.dart';
import 'package:flutter_house_manager/pages/Location/house_form.dart';
import 'package:flutter_house_manager/pages/Location/location_list.dart';
import 'package:flutter_house_manager/pages/Location/room_list.dart';
import 'package:flutter_house_manager/pages/Login/index.dart';
import 'package:flutter_house_manager/pages/NotFound/not_found.dart';
import 'package:flutter_house_manager/pages/NoticeDetail/index.dart';
import 'package:flutter_house_manager/pages/Profile/index.dart';
import 'package:flutter_house_manager/pages/TabsPage/index.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    title: '房屋管理系统',
    initialRoute: '/',
    routes: {
      '/': (context) => const TabsPage(),
      '/noticeDetail': (context) => const NoticeDetail(),
      "/login": (context) => const LoginPage(),
      // "/profile": (context) => const ProfilePage(),
    },
    onGenerateRoute: (settings) {
      String token = tokenManager.getToken();
      if (token.isEmpty) {
        return MaterialPageRoute(
            builder: (context) => LoginPage(toName: settings.name));
      }
      if (settings.name == '/profile') {
        return MaterialPageRoute(builder: (context) => const ProfilePage());
      }
      if (settings.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }
      if (settings.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }
      if (settings.name == "/building_list") {
        return MaterialPageRoute(
          builder: (context) => const BuildingList(),
        );
      }
      if (settings.name == "/room_list") {
        return MaterialPageRoute(
          builder: (context) => const RoomList(),
        );
      }
      if (settings.name == "/house_form") {
        String id = "";
        // ignore: unnecessary_null_comparison
        if (settings != null && settings.arguments != null) {
          id = (settings.arguments as Map)["id"];
        }
        return MaterialPageRoute(
          // settings: settings,
          builder: (context) =>  HouseForm(id:id),
        );
      }
      if (settings.name == "/house_detail") {
        return MaterialPageRoute(
          // 路由的参数
          settings: settings,
          builder: (context) => const HouseDetail(),
        );
      }
      // 兜底返回 当什么都匹配不了的时候 匹配404页面
      return MaterialPageRoute(
        builder: (context) => const NotFound(),
      );
    },
  ));
}
